//
//  historyMoneyViewController.m
//  忘不了
//
//  Created by rimi on 16/7/15.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "historyMoneyViewController.h"
#import "SDAutoLayout.h"
#import "UIColor+Expand.h"

@interface historyMoneyViewController ()<UIScrollViewDelegate>
@property (nonatomic,assign)int money1;//记录每个月的总开支
@property (nonatomic,assign)int money2;
@property (nonatomic,assign)int money3;
@property (nonatomic,assign)int money4;
@property (nonatomic,assign)int money5;
@property (nonatomic,assign)int money6;
@property (nonatomic,assign)int money7;
@property (nonatomic,assign)int money8;
@property (nonatomic,assign)int money9;
@property (nonatomic,assign)int money10;
@property (nonatomic,assign)int money11;
@property (nonatomic,assign)int money12;

@property (nonatomic,assign)CGFloat index;//控件宽度
@property (nonatomic,strong)NSArray *totalMoneyArr;//各月总金额组成的数组
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView *lineView1;
@property (nonatomic,strong)UIView *lineView2;
@end

@implementation historyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.8275 green:0.7961 blue:0.7216 alpha:1.0];
    self.title = @"各月支出";
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    _totalMoneyArr = [NSArray array];
    
    [self loadUI];
}

- (void)loadUI {
    //关掉苹果的透视机制
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _scrollView = [[UIScrollView alloc]init];
    _scrollView.bounds = self.view.bounds;
    _scrollView.center = self.view.center;
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*1.2);
    _scrollView.pagingEnabled = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    //设置代理
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    _lineView1 = [[UIView alloc]init];
    _lineView1.backgroundColor = [UIColor darkGrayColor];
    [self.scrollView addSubview:_lineView1];
    _lineView1.sd_layout
    .leftSpaceToView(self.scrollView,5)
    .rightSpaceToView(self.scrollView,5)
    .topSpaceToView(self.scrollView,110)
    .heightIs(1);
    
    _lineView2 = [[UIView alloc]init];
    _lineView2.backgroundColor = [UIColor darkGrayColor];
    [self.scrollView addSubview:_lineView2];
    _lineView2.sd_layout
    .leftSpaceToView(self.scrollView,60)
    .topSpaceToView(self.scrollView,70)
    .heightIs(40)
    .widthIs(1);
    
    UILabel *monthLabel = [[UILabel alloc]init];
    monthLabel.text = @"月份";
    monthLabel.textColor = [UIColor colorWithRed:0.1216 green:0.1216 blue:0.1765 alpha:1.0];
    monthLabel.textAlignment = NSTextAlignmentCenter;
    monthLabel.backgroundColor = [UIColor clearColor];
    monthLabel.font = [UIFont systemFontOfSize:18];
    [self.scrollView addSubview:monthLabel];
    monthLabel.sd_layout
    .topSpaceToView(self.scrollView,70)
    .leftSpaceToView(self.scrollView,10)
    .widthIs(50)
    .heightIs(30);
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    moneyLabel.text = @"金\t\t\t\t\t额";
    moneyLabel.textColor = [UIColor colorWithRed:0.1216 green:0.1216 blue:0.1765 alpha:1.0];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.backgroundColor = [UIColor clearColor];
    moneyLabel.font = [UIFont systemFontOfSize:18];
    [self.scrollView addSubview:moneyLabel];
    moneyLabel.sd_layout
    .topSpaceToView(self.scrollView,70)
    .centerXIs(self.scrollView.frame.size.width/2)
    .leftSpaceToView(_lineView2,10)
    .rightSpaceToView(self.scrollView,10)
    .heightIs(30);
    
    for (int k=0; k<self.dataArray.count; k++) {
        NSString *dateText = _dataArray[k][@"date"];
        int month = [[dateText substringWithRange:NSMakeRange(5,2)] intValue];
        if (month == 1) {
            _money1 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 2) {
            _money2 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 3) {
            _money3 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 4) {
            _money4 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 5) {
            _money5 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 6) {
            _money6 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 7) {
            _money7 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 8) {
            _money8 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 9) {
            _money9 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 10) {
            _money10 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 11) {
            _money11 += [_dataArray[k][@"money"] intValue];
        }
        if (month == 12) {
            _money12 += [_dataArray[k][@"money"] intValue];
        }
    }
   
    //NSArray 存整型数据
    _totalMoneyArr = @[[NSNumber numberWithInt:_money1],
                       [NSNumber numberWithInt:_money2],
                       [NSNumber numberWithInt:_money3],
                       [NSNumber numberWithInt:_money4],
                       [NSNumber numberWithInt:_money5],
                       [NSNumber numberWithInt:_money6],
                       [NSNumber numberWithInt:_money7],
                       [NSNumber numberWithInt:_money8],
                       [NSNumber numberWithInt:_money9],
                       [NSNumber numberWithInt:_money10],
                       [NSNumber numberWithInt:_money11],
                       [NSNumber numberWithInt:_money12]];
    NSLog(@"%@",_totalMoneyArr);
    
    NSArray *Arr1 = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二"];
    for (int i=0; i<12; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = Arr1[i];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:16];
        label.backgroundColor = [UIColor colorWithRed:0.1647 green:0.1647 blue:0.2314 alpha:1.0];
        label.tag = 600 + i;
        label.layer.cornerRadius = self.view.frame.size.width*0.1/2;
        label.layer.masksToBounds = YES;
        [self.scrollView addSubview:label];
        label.sd_layout
        .widthRatioToView(self.scrollView,0.1)
        .heightEqualToWidth()
        .leftSpaceToView(self.scrollView,20)
        .topSpaceToView(self.scrollView,115+self.scrollView.frame.size.width*0.1*i+10*i);
    }
    for (int m=0; m<12; m++) {
        if (_dataArray.count == 0) {
            _index = 0;
        } else {
            _index = 0;
            //取 NSArray 中的数据并转成 int 型
            _index = [self setWidth:[(NSNumber *)[_totalMoneyArr objectAtIndex:m] intValue]];
        }
        UIView *backView = [[UIView alloc]init];
        backView.backgroundColor = [UIColor specialRandomColor];
        backView.tag = 700 + m;
        [self.scrollView addSubview:backView];
        backView.sd_layout
        .widthIs(_index)
        .heightIs(self.scrollView.frame.size.width*0.1)
        .leftSpaceToView(self.scrollView,25+self.scrollView.frame.size.width*0.1)
        .topSpaceToView(self.scrollView,115+self.scrollView.frame.size.width*0.1*m+10*m);
    }
    for (int j=0; j<12; j++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = [NSString stringWithFormat:@"¥%@",_totalMoneyArr[j]];
        label.textColor = [UIColor brownColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:18];
        label.backgroundColor = [UIColor clearColor];
        label.tag = 800 + j;
        [self.scrollView addSubview:label];
        label.sd_layout
        .rightSpaceToView(self.scrollView,10)
        .heightIs(self.scrollView.frame.size.width*0.1)
        .leftSpaceToView([self.scrollView viewWithTag:700+j],0)
        .topSpaceToView(self.scrollView,115+self.scrollView.frame.size.width*0.1*j+10*j);
    }
}

//根据金额大小判定长度
- (CGFloat)setWidth:(int)money {
    CGFloat viewWidth = 0;
    if (money == 0) {
        viewWidth = 0;
    }
    if (money > 0 && money < 50) {
        viewWidth = self.view.frame.size.width*0.1;
    }
    if (money >= 50 && money < 200) {
        viewWidth = self.view.frame.size.width*0.15;
    }
    if (money >= 200 && money < 500) {
        viewWidth = self.view.frame.size.width*0.2;
    }
    if (money >= 500 && money < 1000) {
        viewWidth = self.view.frame.size.width*0.25;
    }
    if (money >= 1000 && money < 2000) {
        viewWidth = self.view.frame.size.width*0.3;
    }
    if (money > 2000 && money < 5000) {
        viewWidth = self.view.frame.size.width*0.4;
    }
    if (money >= 5000) {
        viewWidth = self.view.frame.size.width*0.5;
    }
    return viewWidth;
}


@end
